#! /bin/bash

WORKPATH=$(cd "$(dirname "$0")"; pwd)
TMPDIR="tmp"
OUT_TARGET="rawinfo_key"
#SIGKEY="verified_rsa_key.pem"

KEY_ORIGIN_NAME=("GreyStick" "Supplier" "Descmo" "Scomo")
KEY_WRITE_NAME=("GreyStick" "Supplier" "DESCOMO" "SCOMO")
PUBLIC_KEY="Kpub"
PEM="pem"
CA_ORIGIN_NAME="vnext_root.x509.pem"
CA_WRITE_NAME="vNextRootCA.crt"
main() {
    clean

    mkdir ${WORKPATH}/${TMPDIR}

    #copy keys
    for i in ${!KEY_ORIGIN_NAME[@]}; do
        cp $2/${PUBLIC_KEY}${KEY_ORIGIN_NAME[$i]}.${PEM} ${WORKPATH}/${TMPDIR}/${PUBLIC_KEY}${KEY_WRITE_NAME[$i]}.${PEM}
        if [[ $? != 0 ]]; then
            echo "No pub key:${PUBLIC_KEY}${KEY_ORIGIN_NAME[$i]}.${PEM}, in $2"
            return 1
        fi
    done

    #copy root ca
    cp $2/${CA_ORIGIN_NAME} ${WORKPATH}/${TMPDIR}/${CA_WRITE_NAME}
    if [[ $? != 0 ]]; then
        echo "No root ca: ${CA_ORIGIN_NAME}, in $2"
        return 1
    fi

    if [ ! -f $1 ]; then
        echo "No pri key: $1"
        return 1
    fi

    ${WORKPATH}/make_rawinfo --outfile ${WORKPATH}/${OUT_TARGET} --inkeypath ${WORKPATH}/${TMPDIR}/ --insigkey $1
}

clean() {
    rm -rf ${WORKPATH}/${TMPDIR}
    rm -rf ${WORKPATH}/${OUT_TARGET}
}


main $*
